#include <string.h>
#include "gkt_os.h"
#include "gkt_system.h"
#include "gkt_keypad.h"
#include "gkt_fpsensor.h"
#include "gkt_rfid.h"
#include "gkt_door.h"
#include "window_common.h"

#define WINDOW_COMMON_MASKABLE_EVENT_BITS	\
	(((1U << (GKT_EVENT_MAX - GKT_EVENT_MASKABLE)) - 1) << GKT_EVENT_MASKABLE)
#define WINDOW_COMMON_MASKABLE_EVENT_BITS_NK	\
	(WINDOW_COMMON_MASKABLE_EVENT_BITS & (~(1U << GKT_EVENT_KEY)))
void window_common_mask(uint32_t mask_bits)
{
	if (mask_bits & (1U << GKT_EVENT_CARD))
		gkt_event_mask_single(GKT_EVENT_CARD, 0);
	gkt_event_mask(mask_bits);

	if (!(mask_bits & (1U << GKT_EVENT_KEY)))
		gkt_key_mask_partial(0);
	gkt_event_unmask(~mask_bits);
}
void window_common_mask_all(void)
{
	window_common_mask(WINDOW_COMMON_MASKABLE_EVENT_BITS);
}

#define WINDOW_COMMON_ALL_KEYS_MASK	((1U << GKT_KEY_MAX) - 1)
void window_common_mask_except_keys(uint32_t unmask_keys_bits)
{
	uint32_t masked_key_bits;

	gkt_event_mask(WINDOW_COMMON_MASKABLE_EVENT_BITS_NK);

	if (unmask_keys_bits & WINDOW_COMMON_ALL_KEYS_MASK) {
		masked_key_bits = WINDOW_COMMON_ALL_KEYS_MASK & (~unmask_keys_bits);
		gkt_key_mask_partial(masked_key_bits);
		gkt_event_unmask_single(GKT_EVENT_KEY);
	}
	else
		gkt_event_mask_single(GKT_EVENT_KEY, 0);
}
void window_common_mask_except_return(void)
{
	uint32_t unmask_keys_bits;

	unmask_keys_bits = 1U << GKT_KEY_RETURN;
	window_common_mask_except_keys(unmask_keys_bits);
}
void window_common_mask_except_cancel_ok(void)
{
	uint32_t unmask_keys_bits;

	unmask_keys_bits = (1U << GKT_KEY_CANCEL) | (1U << GKT_KEY_OK);
	window_common_mask_except_keys(unmask_keys_bits);
}
void window_common_mask_except_updn_cancel_ok(void)
{
	uint32_t unmask_keys_bits;

	unmask_keys_bits = (1U << GKT_KEY_UP) | (1U << GKT_KEY_DOWN) 
						| (1U << GKT_KEY_CANCEL) | (1U << GKT_KEY_OK);
	window_common_mask_except_keys(unmask_keys_bits);
}


typedef struct _window_common_krt_state {
	uint32_t	depth;
} window_common_krt_state_s;
static window_common_krt_state_s s_window_common_krt_state;

#if defined(GKT_CONFIG_DEBUG_ENABLE) && GKT_CONFIG_DEBUG_ENABLE
void _window_common_control_key_real_trim(const char *prefix, int enable)
{
	gkt_trace("KRT - (%s) enable(%d), depth(%u)\n",
		prefix, enable,	s_window_common_krt_state.depth);

	if (enable) {
		if (s_window_common_krt_state.depth > 0) {
			s_window_common_krt_state.depth--;
			if (!s_window_common_krt_state.depth)
				gkt_keypad_control(GKT_KEYPAD_CTRL_REAL_TRIM, GKT_KEYPAD_REAL_TRIM_RESTORE);
		}
		else {
			gkt_error("window_common_control_key_real_trim: underflow!\n");
			GKT_ASSERT(0);
		}
	} else {
		if (!s_window_common_krt_state.depth)
			gkt_keypad_control(GKT_KEYPAD_CTRL_REAL_TRIM, GKT_KEYPAD_REAL_TRIM_BACKUP);
		s_window_common_krt_state.depth++;
	}
}
#else
void _window_common_control_key_real_trim(int enable)
{
	gkt_trace("KRT - enable(%d), depth(%u)\n",
		enable, s_window_common_krt_state.depth);

	if (enable) {
		if (s_window_common_krt_state.depth > 0) {
			s_window_common_krt_state.depth--;
			if (!s_window_common_krt_state.depth)
				gkt_keypad_control(GKT_KEYPAD_CTRL_REAL_TRIM, GKT_KEYPAD_REAL_TRIM_RESTORE);
		}
		else {
			gkt_error("window_common_control_key_real_trim: underflow!\n");
			GKT_ASSERT(0);
		}
	} else {
		if (!s_window_common_krt_state.depth)
			gkt_keypad_control(GKT_KEYPAD_CTRL_REAL_TRIM, GKT_KEYPAD_REAL_TRIM_BACKUP);
		s_window_common_krt_state.depth++;
	}
}
#endif

/* door tamper alarm */
#if defined(GKT_CONFIG_DOOR_TAMPER_ENABLE) \
	&& GKT_CONFIG_DOOR_TAMPER_ENABLE
void window_door_tamper_stop(void)
{
	if(g_window_ram_data.door_tamper_detect)
		gkt_door_tamper_stop();
}

#if	defined(GKT_CONFIG_FACE_ID_ENABLE)	&& (GKT_CONFIG_FACE_ID_ENABLE)
#if	defined(GKT_CONFIG_PROXIMITY_SENSOR_ENABLE)	&& (GKT_CONFIG_PROXIMITY_SENSOR_ENABLE)
#include "gkt_pr_sensor.h"
void window_pr_sensor_handle(void)
{
	gkt_printf("window_pr_sensor_handle enter, pr_power_state=%d\n", g_window_retmem_data.pr_power_state);
	gkt_user_amount_s amount = {0, 0};
	int retval;
	retval = gkt_user_get_amount(&amount, GKT_USER_AUTH_MAX, 
					GKT_USER_TYPE_MAX);
	if ((GKT_SUCCESS == retval) && (amount.used_nums == 0)){
		gkt_pr_sensor_set_threshold(gp_window_data->pr_sensor_state, 1);
		g_window_retmem_data.pr_power_state = GKT_PR_SENSOR_POWER_ON;
	}
	else if((GKT_SUCCESS == retval) && (amount.used_nums > 0)){
		gkt_pr_sensor_set_threshold(gp_window_data->pr_sensor_state, 1);
		g_window_retmem_data.pr_power_state = GKT_PR_SENSOR_POWER_ON;

		retval = gkt_user_get_amount(&amount, GKT_USER_AUTH_MAX, 
								GKT_USER_TYPE_FACE_ID);
		if ((GKT_SUCCESS == retval) && (amount.used_nums == 0)) {
#if defined(GKT_CONFIG_PALM_ENABLE) && GKT_CONFIG_PALM_ENABLE
			retval = gkt_user_get_amount(&amount, GKT_USER_AUTH_MAX, 
								GKT_USER_TYPE_PALM);
			if ((GKT_SUCCESS == retval) && (amount.used_nums == 0))
#endif
			{
				gkt_printf("window_pr_sensor_handle. shutdown: used_nums>0 && face_id_user and palm_user=0\n");
				gkt_pr_sensor_set_threshold(GKT_PR_SENSOR_SENSE_SHUTDOWN, 1);
				g_window_retmem_data.pr_power_state = GKT_PR_SENSOR_POWER_OFF;
			}
		}
	}
	else {
		gkt_printf("window_pr_sensor_handle. shutdown: retval=%d, amount.used_nums=%d\n", retval, amount.used_nums);
		gkt_pr_sensor_set_threshold(GKT_PR_SENSOR_SENSE_SHUTDOWN, 1);//err
		g_window_retmem_data.pr_power_state = GKT_PR_SENSOR_POWER_OFF;
	}
	uint32_t value;
	BPK_ReadKey(&value, 1, GKT_RETMEM_PROXIMITY_POWER_STATE_COUNT);
	value &= 0xFFFF0000;
	value |= g_window_retmem_data.pr_power_state; 
	BPK_WriteKey(&value, 1, GKT_RETMEM_PROXIMITY_POWER_STATE_COUNT);
}
#endif
#endif

#endif

